package books4all

import java.util.Date;

class User {

	transient springSecurityService

	String username
	String password
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired
	
	String mail, sexe
	Date ddn
	Image avatar

	static hasMany=[amis: User, images : Image, biblio : Livre, messagerie : Message]

	static constraints = {
		username blank: false, unique: true
		password blank: false/*, size: 6..16*/
		
		mail blank: false, email: true
		avatar nullable:true
		ddn  nullable: true, blank: false, max: new Date().minus(3650)
		sexe  nullable: true, inList: ['M','F']
		biblio nullable: true
		amis nullable: true
		images nullable: true
		messagerie nullable: true
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
